package se.opendataexchange.rockwell;

import java.io.IOException;

import se.opendataexchange.common.polling.IPollData;
import se.opendataexchange.ethernetip4j.Log;
import se.opendataexchange.ethernetip4j.clx.SimpleLogixCommunicator;

public class LogixCommunicator extends SimpleLogixCommunicator implements IPollData {

	public LogixCommunicator(String host, int port) throws IOException {
		super(host, port);
	}

	@Override
	public Object[] readData(String item, int arraySize) {
		try{
			Object[] datas = (Object[])super.read(item, arraySize);
			return datas;
		}catch(Exception ex){
			ex.printStackTrace();
			return null;
		}

	}

	@Override
	public Object readData(String item) {
		try{
			Object data = super.read(item);
			return data;
		}catch(Exception ex){
			ex.printStackTrace();
			return null;
			
		}
	}

	@Override
	public Object[] readData(String[] items) {
		try{
			Object[] datas = super.read(items);
			return datas;
		}catch(Exception ex){
			ex.printStackTrace();
			Log.p("Error reading: ");
			for (String s : items){
				Log.p(s);
			}
			return null;
		}
	}

	@Override
	public void writeData(String item, Object data) {
		try{
			super.write(item, data, 1);
		}catch(Exception ex){
			
		}
	}

	@Override
	public void writeData(String item, Object[] datas) {
		try{
			super.write(item,(Object)datas,datas.length);
		}catch(Exception ex){
			
		}
		
	}

	@Override
	public void writeData(String[] items, Object[] datas) {
		if(items.length!=datas.length)return;
		try{
			for(int x=0;x<items.length;x++){
				super.write(items[x],datas[x],1);
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
	}
	

}
